package com.sunjob.mroresumeproject.busactioncontroller;


import com.sunjob.common.domain.mroresume.TFBus;
import com.sunjob.common.result.Result;

import com.sunjob.common.util.MROUtils;
import com.sunjob.mroresumeproject.service.TFBusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RequestMapping("/TF")
@RestController
@CrossOrigin
@Api(tags = "机车改造测试接口")
public class TFBusController {


    @Autowired
    TFBusService tfBusService;

    //分页，上下页
    @RequestMapping("/getpages")
    public Result getpages(@RequestParam(value = "pages2")String pages2,String types){

        int page2=Integer.parseInt(pages2);

        if(page2<1){
            page2=1;
        }



        int size=5;
        //部件定检履历查出部件定检记录总条数
        System.out.println(types);
        int count=tfBusService.querycount(types);
        System.out.println(count);
        //求总页数
        int pageCount =((count-1)/size)+1;
        System.out.println(pageCount);
        if(page2>pageCount){
            page2=pageCount;
        }

//        //第一页放五条数据
//        int pages=(page2-1)*size;


        List<TFBus> list=tfBusService.querytransform((page2-1)*size,size,types);

//        System.out.println(list);
        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;

    }


    //首页加载出五条数据
    @RequestMapping("/selecttransform")
    public Result selecttransform(@RequestParam(value ="types")String types){

//        System.out.println(types.length());
        int page2=1;
        int size=5;

        //部件定检履历查出部件定检记录总条数
        int count=tfBusService.querycount(types);
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;



        //第一页放五条数据
        int pages=(page2-1)*size;
        List<TFBus> list=tfBusService.querytransform(pages,size,types);

        
        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;
    }





    //搜索框输入条件查询
    @RequestMapping("/selectTFcontent")
    @ApiOperation(value = "根据transformid查询")
    public Result selectTFcontent(@ApiParam(value = "id查询",required =false) Long transformid){
        //搜索框条件查询可以是部件定检编号或者部件名称
        List<TFBus> list=tfBusService.querytransform2(transformid);

        //result返回查询出的部件定检履历记录
        Result result=new Result();
        result.setData(list);
        System.out.println(result);
        return result;


    }




    //新增
    @RequestMapping("/inserttransform")
    @ApiOperation(value = "机车改造新增")
    public void inserttransform(TFBus tfBus) {
        System.out.println(tfBus.getTransformid());

        //部件定检履历记录新增
        System.out.println(tfBus.getStarttime2());
        System.out.println(tfBus.getEndtime2());
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime ldt = LocalDate.parse(tfBus.getStarttime2(),df).atStartOfDay();
        LocalDateTime ldt2 = LocalDate.parse(tfBus.getEndtime2(),df).atStartOfDay();
        if(tfBus.getFailreason()==""){
            tfBus.setFailreason("无");
            tfBus.setTFstate("完成");

        }
       else {

            tfBus.setTFstate("失败");


        }
        tfBus.setStarttime(ldt);
        tfBus.setEndtime(ldt2);
        tfBus.setStatus(1);

        tfBusService.inserttransform(tfBus);


    }

    @RequestMapping("/insertTFBus")
    public void insertTFBus(TFBus tfBus) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Date date = sdf.parse(tfBus.getStarttime2().split("\"")[1]);
//        System.out.println(date);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        tfBus.setStarttime(instant.atZone(zoneId).toLocalDateTime());

        if (tfBus.getFailreason()==""){
            tfBus.setFailreason("无");
            tfBus.setTFstate("完成");
        }
        else {
            tfBus.setTFstate("失败");
        }
        Date date1 = new Date();
        Instant instant1 = date1.toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();

        tfBus.setEndtime(instant1.atZone(zoneId1).toLocalDateTime());
        tfBus.setStatus(1);

        System.out.println(tfBus);

        tfBusService.inserttransform(tfBus);
    }



    //编辑，详情查看按钮，根据transformid查询
    @RequestMapping("returntransform")
    @ApiOperation(value = "根据transformid查询")
    public Result returntransform(@ApiParam(value = "transformid")  String transformid){
        System.out.println(transformid);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");

//         List<TFBus> tfBuses=tfBusService.querytransformone(transformid);
        TFBus tfBuses=tfBusService.query2(transformid);
        String st=df.format(tfBuses.getStarttime());
        String ed=df.format(tfBuses.getEndtime());
        tfBuses.setStarttime2(st);
        tfBuses.setEndtime2(ed);
        Result result=new Result();
        result.setData(tfBuses);
        return result;
    }

    //对根据查询出的机车改造数据做修改
    @RequestMapping("/updatetransform")
    @ApiOperation(value = "根据transformid修改")
    public void updatetransform(TFBus tfBus){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime ldt = LocalDate.parse(tfBus.getStarttime2(),df).atStartOfDay();
        LocalDateTime ldt2 = LocalDate.parse(tfBus.getEndtime2(),df).atStartOfDay();
        System.out.println(tfBus);
         if(tfBus.getFailreason()==""){

           tfBus.setTFstate("完成");
         }
         else {
             tfBus.setTFstate("失败");
         }
        System.out.println(tfBus);
         tfBus.setStarttime(ldt);
         tfBus.setEndtime(ldt2);
        tfBusService.updatetransform(tfBus);

    }

    //根据transformid删除修改这条记录的状态
    @RequestMapping("/deletetransform")
    @ApiOperation(value = "根据transformid删除")
    public void deletetransform(@ApiParam(value = "transformid")String transformid){

        //修改这条记录状态，0为消失，1为存在
        tfBusService.deletetransform(transformid);


//        //删除后查询部件定检履历记录
//        List<TFBus>tfBuses=tfBusService.querytransform5();
//        Result result=new Result();
//        result.setData(tfBuses);
//        return result;
    }

    @RequestMapping("/deletetransform2")
    public void deletetransform2(String transformid2){
        System.out.println(transformid2);
      String[] a=  transformid2.split(",");
        String[] c=  a[0].split("\\[");
        System.out.println(c[0]);
        System.out.println(c.length);
     String[] d = a[a.length-1].split("\\]");
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                a[0]=c[1];

            }
            if (i==a.length-1){
                a[a.length-1]=d[0];
            }
            tfBusService.deletetransformone(a[i]);
        }
    }


}
